module rank {

    export class NextPass extends egret.Sprite {

        public static frame;

        public static line;

        private bigRank: egret.DisplayObjectContainer;

        private infos: Array<ScoreItem>;

        public static WIDTH: number = 120;
        public static HEIGHT: number = 1334;
        public constructor() {
            super();
            NextPass.cutHeight = (NextPass.HEIGHT - NextPass.bottomHeight) / NextPass.SCORE_GAP;
            //console.log("cutHeight:",NextPass.cutHeight);
            //树枝120/750
        }

        public dispose(): void {
            // if (this.bigRank != null) {
            //     this.bigRank.dispose();
            // }
        }
        private myScore: number;
        //树底的高度
        private static readonly bottomHeight = 233;
        //1分对应树的高度
        private static cutHeight = 100;
        //树干显示的分数跨越
        private static readonly SCORE_GAP = 13;
        private friendArr = [];
        public update(infos: Array<ScoreItem>, command: number, myScore: number): void {
            this.infos = infos;
            this.myScore = myScore;
            //console.log("infos:", this.infos);
            this.showRank();
        }

        private showRank(): void {
            if (!this.bigRank) {
                this.bigRank = new egret.DisplayObjectContainer();
                this.addChild(this.bigRank);
                this.createUserItem();
            } else {
                this.updateUserItem();
            }
            this.bigRank.visible = true;
            
        }

        private checkFriendArr () {
            for (var i = 0; i < this.friendArr.length; i++) {
                if (this.friendArr[i].score <= this.myScore) {
                    this.friendArr[i].visible = false;
                    this.friendArr.splice(i,1);
                }
            }
        }

        private createUserItem () {
            var bottom = NextPass.HEIGHT - NextPass.bottomHeight - 85;
            this.checkFriendArr();
            for (var i = 0 ; i < this.infos.length ; i++) {
                var info = this.infos[i], score = Number(info.score);
                var y = NextPass.HEIGHT - NextPass.bottomHeight - (score - this.myScore) * NextPass.cutHeight;
                if (y < bottom) {
                    var loader = new egret.ImageLoader();
                    loader.once(egret.Event.COMPLETE, this.drawPt.bind(this, score, info.openid, info.nick, y), this);
                    loader.load(info.avatarUrl);
                }
            }
        }

        private updateUserItem () {
            var bottom = NextPass.HEIGHT - NextPass.bottomHeight - 85;
            this.checkFriendArr();
            for (var i = 0 ; i < this.infos.length ; i++) {
                var info = this.infos[i], score = Number(info.score);
                var index = this.checkExist(info.openid);
                if (index != -1) {
                    //用户存在，更新位置
                    var y = NextPass.HEIGHT - NextPass.bottomHeight - (score - this.myScore) * NextPass.cutHeight;
                    if (y > bottom) {
                        //移除该用户
                        this.friendArr[index].visible = false;
                    } else {
                        this.friendArr[index].y = y;
                    }
                } 
            }
        }

        private checkExist (openid) {
            for (var i = 0 ; i < this.friendArr.length ; i++) {
                if (this.friendArr[i].openid == openid) {
                    return i;
                }
            }
            return -1;
        }

        private drawPt (score, openid, name, y, event) {
            var texture = new egret.Texture(), pt = new egret.Bitmap(), ctn = new egret.DisplayObjectContainer();
            var bg = new egret.Bitmap(NextPass.frame), name_label = new egret.TextField(), score_label = new egret.TextField();
            ctn.addChild(bg);
            texture._setBitmapData(event.currentTarget.data);
            pt = new egret.Bitmap(texture);
            pt.height = pt.width = 60;
            pt.x = 39;
            pt.y = 33;
            ctn.addChild(pt);

            name_label.text = name && name.length > 6 ? name.substring(0, 5) + "..." : name && name.substring(0, 6);
            name_label.textColor = 0x000000;
            name_label.size = 16;
            name_label.y = 97;
            name_label.verticalAlign = "top";
            name_label.width = bg.width;
            name_label.textAlign = "center";
            ctn.addChild(name_label);
            
            score_label.text = score;
            score_label.textColor = 0x000000;
            score_label.size = 16;
            score_label.y = name_label.y + name_label.size + 3;
            score_label.verticalAlign = "top";
            score_label.width = bg.width;
            score_label.textAlign = "center";
            ctn.addChild(score_label);

            ctn.y = y;
            ctn["openid"] = openid;
            this.friendArr.push(ctn);
            this.bigRank.addChild(ctn);
        }
    }

}